var WxParse = require('../../wxParse/wxParse.js');
//获取应用实例
var app = getApp()
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0
  },
  onLoad: function (e) {
    var that = this;
    if (e && e.type) {
      that.setData({
        curType: e.type
      })
      console.log(that.data.curType)
      var title = '';
      if (e.type == 'collect') {
        title = '我的收藏';
      } else if (e.type == 'browse') {
        title = '浏览记录';
      } else if (e.type == 'mobile') {
        title = '拨号记录';
      }
      app.setNavtitle(title);
    }
    that.requestShoplist();
  },
  onShow: function () {
    var that = this;
    var curType = that.data.curType;
    var shopList = that.data.shopList ? that.data.shopList:[];
    if (wx.getStorageSync('removeCollectid') && curType == 'collect' && shopList.length>0){
      var id = wx.getStorageSync('removeCollectid');
      console.log("需要移除的元素id");
      for(var i=0;i<shopList.length;i++){
        if (shopList[i].id==id){
          shopList.splice(i,1);
        }
      }
      that.setData({
        shopList: shopList
      })
      wx.removeStorageSync('removeCollectid');
    }
  },
  requestShoplist: function () {
    var that = this;
    var data = {};
    var page = that.data.page;
    data.map = 'applet_mobile_record_list';
    data.type = that.data.curType;
    data.page = page;
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(page);
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.shopList ? that.data.shopList : [];
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            shopList: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.shopList);
        } else {
          if (page <= 0) {
            that.setData({
              shopList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    });
  },
  requestDelrecord: function (id, index) {
    var that = this;
    var data = {
      map: 'applet_mobile_set_record',
      sid: id,
      type: that.data.curType,
      delete: 1
    };
    console.log(data);
    wx.showLoading({
      title: '加载中',
      mask: true,
      time: 100000
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var shopList = that.data.shopList;
          shopList.splice(index, 1);
          that.setData({
            shopList: shopList
          })
          app.errorTip(that, res.data.data.msg, 2000);
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  delShop: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '确定删除该条数据吗？',
      success: function (res) {
        if (res.confirm) {
          that.requestDelrecord(id, index);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  toggleType: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    var title = e.currentTarget.dataset.title;
    that.setData({
      curType: type
    })
    app.setNavtitle(title);
    that.requestShoplist();
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    that.requestShoplist();
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestShoplist();
    }
  },
  makeCall: function (e) {
    var that = this;
    var mobile = e.currentTarget.dataset.mobile;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '',
      content: '拨打电话 ' + mobile,
      confirmText: '拨打',
      confirmColor: '#48C23D',
      success: function (res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: mobile,
            success: function () {
              that.requestCall(id);
            },
            fail: function () {
              console.log("拨打电话失败！")

            }
          })
        }
      }
    })
  },
  // 查看地图
  openMap: function (e) {
    var latitude = e.currentTarget.dataset.lat;
    var longitude = e.currentTarget.dataset.lng;
    var address = e.currentTarget.dataset.address;
    var name = e.currentTarget.dataset.name;
    wx.openLocation({
      latitude: Number(latitude),
      longitude: Number(longitude),
      name: name,
      address: address,
      scale: 18
    })
  },
  toShopInfoDetail: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var type = that.data.curType;
    var url = '/subpages/shopInfoDetail/shopInfoDetail?id=' + id;
    if (type == 'collect') {
      url = '/subpages/shopInfoDetail/shopInfoDetail?id=' + id + '&&type=' + type
    }
    wx.navigateTo({
      url: url
    })
  },
  requestCall: function (id) {
    var that = this;
    var data = {
      map: 'applet_mobile_set_record',
      sid: id,
      type: 'mobile'
    };
    console.log(data);
    wx.showLoading({
      title: '加载中',
      mask: true,
      time: 100000
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          // app.errorTip(that, res.data.data.msg, 2000);
        } else {
          console.log(res.data)
          // app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
})
